import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import pageStack from '../utils/pageStack'
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('../views/Dashboard.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/calendar',
    name: 'Calendar',
    component: () => import('../views/Calendar.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/diary/:date?',
    name: 'Diary',
    component: () => import('../views/Diary.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/timer',
    name: 'Timer',
    component: () => import('../views/Timer.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/work',
    name: 'Work',
    component: () => import('../views/Work.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/countdown',
    name: 'Countdown',
    component: () => import('../views/Countdown.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/countdown-days',
    name: 'CountdownDays',
    component: () => import('../views/CountdownDays.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/inner',
    name: 'Inner',
    component: () => import('../views/Inner.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/notes',
    name: 'Notes',
    component: () => import('../views/Notes.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/notes/new',
    name: 'NoteNew',
    component: () => import('../views/NoteDetail.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/notes/:id(\\d+)', // 仅匹配数字ID（如 /notes/123）
    name: 'NoteDetail',
    component: () => import('../views/NoteDetail.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  // history: createWebHistory(),
  history: createWebHashHistory(), // 替换为 hash 模式
  routes
})

router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  // 添加到页面栈（排除重定向和根路径，但允许/dashboard即使是重定向而来）
  if (to.path !== from.path && to.path !== '/' && 
      (!to.redirectedFrom || to.path === '/dashboard')) {
    pageStack.push({
      path: to.path,
      name: to.name
    })
  }
  
  // 笔记模块状态管理逻辑
  if (from.name === 'Notes' && (to.name === 'NoteDetail' || to.name === 'NoteNew')) {
    // 从笔记列表页进入详情/编辑页 - 保持状态缓存
    // 状态会在Notes.vue的onUnmounted中自动保存
  } else if (from.name === 'NoteDetail' && to.name === 'Notes') {
    // 从详情/编辑页返回列表页 - 保持状态缓存
    // 状态会在Notes.vue的onMounted中自动恢复
  } else if (to.name === 'Notes' && (from.name === 'Dashboard' || from.path === '/dashboard')) {
    // 从主页进入笔记列表页 - 清除状态缓存，重新加载数据
    sessionStorage.removeItem('notesPageState')
  } else if (to.name === 'Notes' && from.name !== 'NoteDetail' && from.name !== 'NoteNew') {
    // 从其他页面进入笔记列表页 - 清除状态缓存，重新加载数据
    sessionStorage.removeItem('notesPageState')
  }
  
  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    // 重置页面栈
    pageStack.reset()
    next('/login')
  } else if ((to.path === '/login' || to.path === '/register') && authStore.isAuthenticated) {
    // 登录成功后重置页面栈
    pageStack.reset()
    next('/dashboard')
  } else {
    next()
  }
})

export default router